﻿using Domain.Examin;
using Domain.Factory_Modeling;
using Domain.InvoiceTicket.Invoicing;
using Domain.InvoiceTicket.Ticket;
using Domain.Material_Management;
using Domain.Process_Procedures;
using Domain.Production_Modules;
using Domain.QualityInspection_Modelling;
using Domain.User;
using Domain.Weather;
using Microsoft.EntityFrameworkCore;

namespace Infratructure
{
    /// <summary>
    /// 数据迁移上下文
    /// </summary>
    public class DateContext : DbContext
    {
        public DateContext(DbContextOptions options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 配置BOM自关联关系
            modelBuilder.Entity<BomModel>()
                .HasOne(b => b.Parent)
                .WithMany(b => b.Children)
                .HasForeignKey(b => b.ParentId)
                .OnDelete(DeleteBehavior.Restrict); // 防止级联删除

            // 配置BOM索引
            modelBuilder.Entity<BomModel>()
                .HasIndex(b => b.BomCode)
                .IsUnique();

            modelBuilder.Entity<BomModel>()
                .HasIndex(b => b.ParentId);

            modelBuilder.Entity<BomModel>()
                .HasIndex(b => b.Level);

            // 配置高德天气实况数据为无键实体（仅用于API响应，不存储到数据库）
            modelBuilder.Entity<GaoDeWeatherLive>()
                .HasNoKey();

            // 配置高德天气预报数据为无键实体（仅用于API响应，不存储到数据库）
            modelBuilder.Entity<GaoDeWeatherForecast>()
                .HasNoKey();

            // 配置高德天气预报单日数据为无键实体（仅用于API响应，不存储到数据库）
            modelBuilder.Entity<GaoDeWeatherCast>()
                .HasNoKey();

            // 忽略高德天气模型中的Lives和Forecast属性，避免关系配置问题
            modelBuilder.Entity<GaoDeWeatherModel>()
                .Ignore(g => g.Lives);
            
            modelBuilder.Entity<GaoDeWeatherModel>()
                .Ignore(g => g.Forecast);
        }
        #region RBAC
        /// <summary>
        /// 账号模块
        /// </summary>
        public DbSet<AccountModel> AccountModel { get; set; }
        /// <summary>
        /// 角色模块
        /// </summary>
        public DbSet<RoleModel> RoleModel { get; set; }
        /// <summary>
        /// 账号角色模块模块
        /// </summary>
        public DbSet<AccountRoleModel> AccountRoleModel { get; set; }
        /// <summary>
        /// 权限模块
        /// </summary>
        public DbSet<PermissionModel> PermissionModel { get; set; }
        /// <summary>
        /// 角色权限模块
        /// </summary>
        public DbSet<RolePermissionModel> RolePermissionModel { get; set; }
        /// <summary>
        /// 部门模块
        /// </summary>
        public DbSet<DepartmentModel> DepartmentModel { get; set; }
        /// <summary>
        /// 角色部门模块
        /// </summary>
        public DbSet<DepartmeRoleModel> DepartmeRoleModel { get; set; }
        /// <summary>
        /// 供应商表
        /// </summary>
        public DbSet<VendorModel> vendorModels { get; set; }
        /// <summary>
        /// 菜单 2025年9月19日15:30:10
        /// </summary>
        public DbSet<SysMenu> SysMenus{ get; set; }
        /// <summary>
        /// 实体 2025年9月19日15:30:22
        /// </summary>
        public DbSet<EntityFieldConfig> EntityFieldConfigs { get; set; }
        /// <summary>
        /// 单据编码规则实体 2025年9月19日19:10:55
        /// </summary>
        public DbSet<DocumentCodeRule> DocumentCodeRules { get; set; }
        

        #endregion
        #region 工艺工序
        /// <summary>
        /// 工序分类
        /// </summary>
        public DbSet<ProcessCategoryModel> ProcessCategoryModels { get; set; }
        /// <summary>
        /// 工艺流程明细
        /// </summary>
        public DbSet<ProcessFlowDetailModel>  ProcessFlowDetailModels { get; set; }
        /// <summary>
        /// 工序流程
        /// </summary>
        public DbSet<ProcessFlowModel> ProcessFlowModels { get; set; }
        /// <summary>
        /// 工艺管理
        /// </summary>
        public DbSet<ProcessManagementModel> ProcessManagementModels { get; set; }
        #endregion
        #region 工厂建模 
        //产线管理
        public DbSet<ProductionLineModel>productionLineModels { get; set; }
        //工位管理
        public DbSet<StationModel> stationModels { get; set; }
        //车间管理
        public DbSet<WorkShopModel> workShopModels { get; set; }
        #endregion
        #region 物料管理
        //产品物料
        public DbSet<ProductMaterialsModel> productMaterialsModels { get; set; }
        //物料群组
        public DbSet<MaterialGroupModel> materialGroupModels { get; set; }
        //BOM清单
        public DbSet<BomModel> bomModels { get; set; }
        //物料申请单
        public DbSet<MaterialAskModel> materialAskModels { get; set; }
        #endregion
        #region 仓库管理
        /// <summary>
        ///仓库
        /// </summary>
        public DbSet<WarehouseModel> WarehouseModels { get; set; }

        /// <summary>
        /// 物料库存
        /// </summary>
        public DbSet<MaterialInventoryModel> MaterialInventoryModels { get; set; }
        /// <summary>
        /// 群组库存
        /// </summary>
        public DbSet<GroupInventoryModel>groupInventoryModels { get; set; }


        /// <summary>
        ///生产工单表
        /// </summary>
        public DbSet<ProduceModel> ProduceModels { get; set; }
        /// <summary>
        /// 客户表
        /// </summary>
        public DbSet<ClientModel> ClientModels { get; set; }
        /// <summary>
        /// 物料出库入库
        /// </summary>
        public DbSet<MaterialOutInModel> materialOutInModels { get; set; }
        /// <summary>
        /// 物料采购单
        /// </summary>
        public DbSet<PurchaseOrderModel> purchaseOrderModels { get; set; }
        /// <summary>
        /// 入库申请单
        /// </summary>
        public DbSet<MaterialOutAskModel>materialOutAskModels { get; set; }
        /// <summary>
        /// 群组出库入库
        /// </summary>
        public DbSet<GroupOutInModel> groupOutInModels { get; set; }
        /// <summary>
        /// 订单表
        /// </summary>
        public DbSet<OrderModel>orderModels { get; set; }
        /// <summary>
        /// 计划排程
        /// </summary>
        public DbSet<SchedulingModel> schedulingModels { get; set; }
        #endregion

        #region 质量管理ls

        //public DbSet<GroupInventory> GroupInventories { get; set; }

        public DbSet<QualityInspection> QualityInspections { get; set; }
        #endregion
        #region 出票到票

        public DbSet<InvoicingModel> InvoicingModels { get; set; }
        public DbSet<InvoicingorderModel> InvoicingorderModels { get; set; }
        public DbSet<TicketModel> TicketModels { get; set; }
        public DbSet<TicketorderModel> TicketorderModels { get; set; }
        #endregion
        
        #region 天气服务
        /// <summary>
        /// 天气数据模型
        /// </summary>
        public DbSet<WeatherModel> WeatherModels { get; set; }
        /// <summary>
        /// 高德天气
        /// </summary>
        public DbSet<GaoDeWeatherModel> gaoDeWeatherModels { get; set; }
        /// <summary>
        /// 高德天气实况数据
        /// </summary>
        public DbSet<GaoDeWeatherLive> gaoDeWeatherLives { get; set; }
        /// <summary>
        /// 高德天气预报数据
        /// </summary>
        public DbSet<GaoDeWeatherForecast> gaoDeWeatherForecasts { get; set; }
        /// <summary>
        /// 高德天气预报单日数据
        /// </summary>
        public DbSet<GaoDeWeatherCast> gaoDeWeatherCasts { get; set; }
        #endregion
        #region 审批流程
        /// <summary>
        /// 节点审批人配置
        /// </summary>
        public DbSet<ApprovalAssigneeModel> ApprovalAssigneeModels { get; set; }
        /// <summary>
        /// 流程节点实体
        /// </summary>
        public DbSet<ApprovalNodeModel> ApprovalNodeModels { get; set; }
        /// <summary>
        /// 通知记录表
        /// </summary>
        public DbSet<ApprovalNotificationModel> ApprovalNotificationModels { get; set; }
        /// <summary>
        /// 审批流程实例
        /// </summary>
        public DbSet<approvalProcessInstanceModel> ApprovalProcessInstanceModels { get; set; }
        /// <summary>
        /// 审批流程实体
        /// </summary>
        public DbSet<ApprovalProcessModel> ApprovalProcessModels { get; set; }
        /// <summary>
        /// 审批任务历史表 - 已办
        /// </summary>
        public DbSet<ApprovalTaskHistModel> ApprovalTaskHistModels { get; set; }
        /// <summary>
        /// 审批任务表 - 待办
        /// </summary>
        public DbSet<ApprovaltaskModel> ApprovaltaskModels { get; set; }
        #endregion
    }
}
